package leetcode.day;

import java.applet.Applet;
import java.util.Map;
import java.util.Stack;

// 202. 快乐数
public class IsHappy {
    public static void main(String[] args) {
        System.out.println(isHappy(12));
    }

    public static int getNext(int n) {
        int sum = 0;
        while (n > 0) {

            int x = n % 10;
            sum += x * x;
            n /= 10;
        }
        return sum;
    }

    // 利用快慢指针。
    public static boolean isHappy(int n) {
        int slow = n;
        int fast = getNext(n);
        while (slow!=fast){
            slow = getNext(slow);
            fast = getNext(getNext(fast));
        }
        return slow==1;
    }

}


